﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;


namespace BaseHelper.Base
{
    public class UpdateHelper
    {
        private static string Updateing;
        public static string Folder;
        public static string Error;

        public static bool Update(string FilePath)
        {
            Error = "";
            Folder = AppDomain.CurrentDomain.BaseDirectory + "AutoUpdate\\";

            if (File.Exists(Folder))
            {
                try
                {
                    Directory.Delete(Folder, true);
                }
                catch { }
                
            }

            if (!ZipHelper.UnZip(FilePath, Folder))
            { 
                Error = "解析升级包失败！";
                return false;
            }
            string updatePath = Folder + "update.ini";

            if (!File.Exists(updatePath))
            {
                Error = "升级包配置文件丢失！";
                return false;
            }

            Thread.Sleep(1000);
            string autoExe = Folder + IniHelper.Read(updatePath, "info", "execute");

            if (!File.Exists(autoExe))
            {
                Error = "升级包自动升级程序丢失！";
                return false;
            }
            Updateing = AppDomain.CurrentDomain.BaseDirectory + "\\update_complate.tmp";
            if (!File.Exists(Updateing))
            {
                try
                {
                    File.WriteAllText(Updateing, DateTime.Now.ToString());
                }
                catch { }
            }


            Process.Start(autoExe);
            return true;
        }

        public static bool IsComplete()
        {
            return !File.Exists(Updateing);
        }
    }
}
